package com.gjt.InternetAddress.Utils;

import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Created with Intel IDEA.
 * 处理器Web服务器日志文件: 就是把日志文件中的IP 解析出他的主机名并存储
 *
 *
 * @Auther: gjt
 * @Date: 2024/3/21 19:10
 */
public class Weblog {
    public static void main(String[] args) {
        try(InputStream in=  Files.newInputStream(Paths.get("C:\\Users\\23152\\Desktop\\requests.log"))){
            BufferedReader buf = new BufferedReader(new InputStreamReader(in));

            for(String entry = buf.readLine(); entry != null; entry = buf.readLine()){
                // 分解ip地址
               int index = entry.indexOf(' ');
               String ip = entry.substring(0,index);
               String theRest = entry.substring(index);

               try{
                   InetAddress address = InetAddress.getByName(ip);
                   // 可以把解析出来的主机名写入日志中
                   System.out.println("输出"+address.getHostName()+theRest);
               }catch (UnknownHostException e){
                   System.err.println(e.getMessage());
               }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
